// ClientAgreementForm 常用表达式模板
export const ClientAgreementFormExpressions = {
  // 打开对话框的表达式模板
  openDialog: {
    customer: `{{(ctx, deps) => {
  const useDialogInstances = deps.useDialogInstances
  const dialog = useDialogInstances().get('客户')
  dialog.open(ctx)
}}}`,
    agreement: `{{(ctx, deps) => {
  const useDialogInstances = deps.useDialogInstances
  const dialog = useDialogInstances().get('客户协议')
  dialog.open(ctx)
}}}`,
  },

  // 带验证的打开对话框表达式
  openDialogWithValidation: {
    customer: `{{(ctx, deps) => {
  const agreementName = ctx.form.getValuesIn('agreementName')
  const {isMultiAgreet} = deps.getStorage('adminQiankunGlobalState') || {}
  if (isMultiAgreet && !agreementName) {
    deps.antdMsg(ctx.createT('请选择客户协议'), 'error')
    return
  }
  const useDialogInstances = deps.useDialogInstances
  const dialog = useDialogInstances().get('客户')
  dialog.open(ctx)
}}}`,
  },

  // 显示条件表达式
  visibility: {
    multiAgreement: `{{JSON.parse(localStorage.getItem("adminQiankunGlobalState")).data.isMultiAgreet}}`,
    always: '{{true}}',
    never: '{{false}}',
  },

  // 表格选择回调表达式
  tableSelection: {
    customer: `{{(ctx, deps) => {
  const { selectedRows, form } = ctx
  const { name, code } = selectedRows[0] || {}
  form.setValues({
    customerCode: code,
    customerName: name
  })
}}}`,
    agreement: `{{(ctx, deps) => {
  const { selectedRows, form } = ctx
  const { name, code } = selectedRows[0] || {}
  form.setValues({
    agreementCode: code,
    agreementName: name
  })
}}}`,
  },

  // API请求表达式
  apiRequest: {
    customer: `{{async (ctx, deps) => {
  const { request } = deps
  const { params } = ctx
  const res = await request({
    uri: '/api/tenant/agreement/customer/list',
    params: {
      ...params,
    },
    method: 'POST',
  })
  return res
}}}`,
    agreement: `{{async (ctx, deps) => {
  const { request } = deps
  const { params } = ctx
  const res = await request({
    uri: '/api/tenant/agreement/listAgreement',
    params: {
      ...params,
    },
    method: 'POST',
  })
  return res
}}}`,
  },

  // 对话框确认事件
  dialogConfirm: {
    default: `{{(ctx, deps) => {
  console.log('Dialog confirmed:', ctx)
  // 在这里添加确认后的逻辑
}}}`,
  },

  // 表格渲染表达式
  tableRender: {
    dateRange: `{{(ctx) => {
  const record = ctx.record
  return record.startDate && record.endDate && \`\${record.startDate}~\${record.endDate}\`
}}}`,
  },
}

// 表达式编辑器配置
export const ExpressionEditorConfig = {
  // 可用的变量和函数
  context: {
    ctx: {
      type: 'object',
      description: '当前上下文对象',
      properties: {
        form: {
          type: 'object',
          description: '表单实例',
          methods: [
            'getValuesIn',
            'setValues',
            'getFieldValue',
            'setFieldValue',
          ],
        },
        record: {
          type: 'object',
          description: '当前记录数据（表格行数据）',
        },
        selectedRows: {
          type: 'array',
          description: '选中的行数据',
        },
      },
    },
    deps: {
      type: 'object',
      description: '依赖注入对象',
      properties: {
        useDialogInstances: {
          type: 'function',
          description: '获取对话框实例',
          usage: 'deps.useDialogInstances().get("对话框名称")',
        },
        getStorage: {
          type: 'function',
          description: '获取本地存储数据',
          usage: 'deps.getStorage("adminQiankunGlobalState")',
        },
        antdMsg: {
          type: 'function',
          description: '显示Antd消息',
          usage: 'deps.antdMsg("消息内容", "error|success|warning")',
        },
        request: {
          type: 'function',
          description: '发送HTTP请求',
          usage:
            'deps.request({ uri: "/api/path", method: "POST", params: {} })',
        },
      },
    },
  },

  // 常用代码片段
  snippets: [
    {
      label: '打开对话框',
      insertText: `const useDialogInstances = deps.useDialogInstances
const dialog = useDialogInstances().get('对话框名称')
dialog.open(ctx)`,
    },
    {
      label: '表单验证',
      insertText: `if (!ctx.form.getValuesIn('字段名')) {
  deps.antdMsg(ctx.createT('请填写必要信息'), 'error')
  return
}`,
    },
    {
      label: '设置表单值',
      insertText: `ctx.form.setValues({
  字段名: 值
})`,
    },
    {
      label: 'API请求',
      insertText: `const { request } = deps
const { params } = ctx
const res = await request({
  uri: '/api/path',
  params: { ...params },
  method: 'POST',
})
return res`,
    },
  ],
}
